package com.xuecheng.manage_course.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xuecheng.framework.domain.course.CourseBase;
import com.xuecheng.framework.domain.course.Teachplan;
import com.xuecheng.framework.domain.course.ext.CategoryNode;
import com.xuecheng.framework.domain.course.ext.TeachplanNode;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_course.dao.CourseBaseRepository;
import com.xuecheng.manage_course.dao.CourseMapper;
import com.xuecheng.manage_course.dao.TeacherPlanRepository;
import com.xuecheng.manage_course.dao.TeachplanMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author Administrator
 * @version 1.0
 **/
@Service
public class CourseService {

    @Resource
    private TeachplanMapper teachplanMapper;
    @Resource
    private CourseMapper courseMapper;
    @Autowired
    private TeacherPlanRepository teacherPlanRepository;
    @Autowired
    private CourseBaseRepository courseBaseRepository;

    public TeachplanNode findTeachplanList(String courseId) {
        return teachplanMapper.selectTeachplanList(courseId);
    }

    /**
     * 添加课程
     */
    @Transactional
    public ResponseResult addTeachplan(Teachplan teachplan) {
        // 参数检验
        if (teachplan == null || StringUtils.isEmpty(teachplan.getCourseid())
                || StringUtils.isEmpty(teachplan.getPname())){
            return new ResponseResult("参数非法");
        }
        // 课程id
        String courseId = teachplan.getCourseid();
        // 父节点
        String parentId = teachplan.getParentid();
        // 没有选择父节点
        if (StringUtils.isEmpty(parentId)){
            // 根据课程id查询课程根节点
            parentId =  findTeachPlanRoot(courseId);
        }
        // 添加新课程节点计划
        Teachplan teachplanNew = new Teachplan();
        BeanUtils.copyProperties(teachplan,teachplanNew);
        teachplanNew.setParentid(parentId);
        teachplanNew.setCourseid(courseId);
        Optional<Teachplan> optional = teacherPlanRepository.findById(parentId);
        String grade = optional.get().getGrade();
        if (grade.equals("1")){
            teachplanNew.setGrade("2");
        }else {
            teachplanNew.setGrade("3");
        }
        teacherPlanRepository.save(teachplanNew);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    // 根据课程id查询课程根节点,查询不到自动添加根节点
    private String findTeachPlanRoot(String courseId) {
        // 根据课程id查课程名称
        Optional<CourseBase> optional = courseBaseRepository.findById(courseId);
        if (!optional.isPresent()){
            return null;
        }
        CourseBase courseBase = optional.get();
        List<Teachplan> list = teacherPlanRepository.findByCourseidAndParentid(courseId, "0");
        if (list == null || list.size() <= 0){
            // 自动添加根节点
            Teachplan teachplan = new Teachplan();
            teachplan.setCourseid(courseId);
            teachplan.setParentid("0");
            teachplan.setGrade("1");
            teachplan.setStatus("0");
            teachplan.setPname(courseBase.getName());
            teacherPlanRepository.save(teachplan);
            return teachplan.getId();
        }
        return list.get(0).getId();
    }

    /**
     *  查询我的课程列表
     */
    public QueryResponseResult selectCourseList(int page, int size, String companyId) {
        if (page <= 0) {
            page = 1;
        }
        if (size <= 0) {
            size = 10;
        }
        if (StringUtils.isEmpty(companyId)) {
            return new QueryResponseResult("公司id不能为空");
        }
        PageHelper.startPage(page, size);
        Page<Map<String,Object>> resultPage = courseMapper.selectCourseList(companyId);
        QueryResult queryResult = new QueryResult();
        queryResult.setList(resultPage.getResult());
        queryResult.setTotal(resultPage.getTotal());
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    /**
     * 查询课程分类列表
     */
    public CategoryNode selectCategory() {
        CategoryNode categoryNode = courseMapper.selectCategory();
        return categoryNode;
    }
}
